﻿using Inovout.Models;
using Inovout.Repositories;
using NHibernate.Criterion;
using NHibernate.Proxy;
using System;

namespace Inovout.Settings
{
    public class ObjectSettingsRepository : DataObjectRepository<ObjectSettings>
    {
        public ObjectSettings FindByScopeAndKindType(DataObject scope, Type kindType)
        {
            Type scopeType = scope.GetType();
            if (scope is INHibernateProxy)
            {
                scopeType = ((INHibernateProxy)scope).HibernateLazyInitializer.PersistentClass;
            }
            return Find(Restrictions.Eq("ScopeType", scopeType), Restrictions.Eq("ScopeIdentity", scope.Id.ToString())
                , Restrictions.Eq("KindType", kindType));
        }
        public ObjectSettings FindByScopeAndKindType(Enum scope, Type kindType)
        {
            return Find(Restrictions.Eq("ScopeType", scope.GetType()), Restrictions.Eq("ScopeIdentity", scope)
                , Restrictions.Eq("KindType", kindType));
        }

        public ObjectSettings FindByKeyAndKindType(string key, Type kindType)
        {
            return Find(Restrictions.Eq("Key", key).IgnoreCase(),Restrictions.Eq("KindType", kindType));
        }
    }
}
